//
//  BCRecommendCodeController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/29.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCRecommendCodeController.h"
//
#import "GSComomRegisterController.h"
//
#import "BCInvitationCodeView.h"

//request
#import "InvataionCodeCheckRequest.h"

@interface BCRecommendCodeController ()

@property (nonatomic, strong) BCInvitationCodeView *codeView;

@end

@implementation BCRecommendCodeController

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    self.largeTitleLabel.text = GSLoginLocalized(@"Please input invitation code");
    self.subTitleLabel.text = GSLoginLocalized(@"If don't have invitation code, please directly click next step");
    self.subTitleLabel.textColor = GSLOGIN_THEME_COLOR;
    //
    [self setupSubviews];
}
- (void)setupSubviews {
    
    self.codeView = [BCInvitationCodeView new];
    [self.view addSubview:self.codeView];
    [self.codeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.subTitleLabel.mas_bottom).offset(38.0);
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo([BCInvitationCodeView defaultHeight]);
    }];
    //
    [self.view addSubview:self.nextStepButton];
    [self.nextStepButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.codeView.mas_bottom).offset(50.0);
        make.centerX.mas_equalTo(self.view);
        make.size.mas_equalTo([GSCommonLoginButton defaultSize]);
    }];
    self.nextStepButton.enabled = true;
}


- (void)nextStepButtonDidClickedAction {
    [self.view endEditing:true];
    NSString *code = self.codeView.code;
    if (code.length) {
        //校验邀请码
        [self requestInvatationCodeCheck:code];
    }else {
        GSComomRegisterController *registerVC = [GSComomRegisterController new];
        [self.navigationController pushViewController:registerVC animated:true];
    }
}

/**
 验证邀请码
 */
- (void)requestInvatationCodeCheck:(NSString *)code {
    [self.view showLoginToast];
    InvataionCodeCheckRequest *request = [InvataionCodeCheckRequest new];
    request.share_code = code;
    request.uuid = [GeneralParameterManager sharedGeneral].uuid;
    //
    WEAK_SELF
    [request didRequestCompleted:^{
        [wself.view hideLoginToast];
    }];
    [request didRequestSuccessed:^(id result, id page) {
        InvataionCodeCheckResponse *resp = result;
        [wself handleCheckSuccess:resp code:request.share_code];
    }];
    [request didRequestFailed:^(NSError *error) {
        if (error&&wself.view) {
            NSString *msg = error.domain;
            [QMUITips showError:msg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:msg]];
        }
    }];
    [request requestServer];
}

- (void)handleCheckSuccess:(InvataionCodeCheckResponse *)response code:(NSString *)code {
    //纪录下code
    [GSCommonLoginDataManager sharedData].invatationCode = code;
    //
    GSComomRegisterController *registerVC = [GSComomRegisterController new];
    [self.navigationController pushViewController:registerVC animated:true];
}


@end
